package com.dream.dreamhouse.service.Impl;

import cn.hutool.core.util.IdUtil;
import com.dream.dreamhouse.common.entity.IpLocation;
import com.dream.dreamhouse.common.utils.IpUtils;
import com.dream.dreamhouse.entity.Dream;
import com.dream.dreamhouse.entity.vo.DreamVo;
import com.dream.dreamhouse.mapper.DreamMapper;
import com.dream.dreamhouse.service.DreamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author lizhen
 * @date 2025/6/6 17:04
 */
@Service
public class DreamServiceImpl implements DreamService {

    @Resource
    private DreamMapper dreamMapper;

    @Override
    public PageInfo<Dream> getDreamPage(DreamVo dreamVo) {
        PageHelper.startPage(dreamVo.getPageNum(), dreamVo.getPageSize());
        return new PageInfo<Dream>(dreamMapper.getDreamList());
    }

    @Override
    public void addDream(DreamVo dreamVo) {
        // 是否是内网ip
        String IntranetRegion = IpUtils.parseIntranetRegion(dreamVo.getIp());
        if (StringUtils.isNotEmpty(IntranetRegion)) {
            dreamVo.setProvince(IntranetRegion);
        } else {
            IpLocation location = IpUtils.getLocation(dreamVo.getIp());
            dreamVo.setProvince(location.getProvince());
        }
        dreamVo.setId(IdUtil.simpleUUID());
        dreamVo.setTime(new Date());
        dreamVo.setLikeCount(0);
        dreamMapper.addDream(dreamVo);
    }

    @Override
    public PageInfo<Dream> findByLikes(DreamVo dreamVo) {
        PageHelper.startPage(dreamVo.getPageNum(), dreamVo.getPageSize());
        return new PageInfo<>(dreamMapper.findByLikes());
    }

    @Override
    public void increaseLikeCount(DreamVo dreamVo) {
        dreamMapper.increaseLikeCount(dreamVo);
    }

    @Override
    public void decreaseLikeCount(DreamVo dreamVo) {
        dreamMapper.decreaseLikeCount(dreamVo);
    }

    @Override
    public PageInfo<Dream> findByTime(DreamVo dreamVo) {
        PageHelper.startPage(dreamVo.getPageNum(), dreamVo.getPageSize());
        return new PageInfo<>(dreamMapper.findByTime());
    }
}
